<?php

class User {

    public $user_id;
    public $social;


    public $data = array();

    public $toUpdate;
    public $profileLoaded = false;

    public function __construct($user_id, $social = 0) {
        if ($social) {
            // находим реальгый id
            $real_id = $this->getRealId($user_id, $social);
            if (!$real_id) {
                // не зареген
                $this->user_id = $this->create($user_id, $social);
            }
            else $this->user_id = $real_id;
        }
        else $this->user_id = $user_id;

    }

    public function save()
    {
        if(count($this->toUpdate))
        {
            $sqlpart = null;
            foreach($this->toUpdate as $f => $v)
                    $sqlpart[] = '`'.$f.'`=\''.$v.'\'';
            $query = 'INSERT INTO `user` SET `user_id`='.$this->user_id.', '.implode(',',$sqlpart).'
                ON DUPLICATE KEY UPDATE '.implode(',',$sqlpart);
            $this->toUpdate = array();
            
            return _database::query($query);
        }
    }

    public function setProperty($property,$value,$hard = false)
    {
        if(!$this->profileLoaded) return false;
        if(isset($this->data[$property]))
        {
            if($hard || $this->data[$property] != $value)
            {
                $this->toUpdate[$property] = $value;
            }
        }
        $this->data[$property] = $value;
    }

    public function getRealId($user_id, $social) {

            $query = 'SELECT `id` FROM `user_social`
                WHERE `user_id`='.$user_id.
            ' AND `user_type`='.$social;
            $r = _database::torow($query);

            return isset($r['id'])&&$r['id'] ? $r['id'] : false;  
    }


    public function getProfile()
    {
        if($this->profileLoaded) return $this->data;
        $query = 'SELECT * FROM `user` WHERE `user_id`='.$this->user_id;
        $this->data = _database::torow($query);
        $this->profileLoaded = true;
        return $this->data;
    }

	public function AddMoney($cnt)
	{
		$this->getProfile();
		$cnt = (int)max(0,$cnt);
		$old = (int)$this->data['money'];
		$this->setProperty('money', $old + $cnt);
		return true;
	}

	public function DecMoney($cnt)
	{
		$this->getProfile();
		$cnt = (int)max(0,$cnt);
		$old = (int)$this->data['money'];
		if($old - $cnt >= 0)
		{
			$this->setProperty('money', $old - $cnt);
			return true;
		}
		else return false;
	}

	public function AddExp($cnt)
	{
		$this->getProfile();
		$cnt = (int)max(0,$cnt);
		$old = (int)$this->data['exp'];
		$this->setProperty('exp', $old + $cnt);
		return true;
	}

	public function AddWon($cnt)
	{
		$this->getProfile();
		$cnt = (int)max(0,$cnt);
		$old = (int)$this->data['won_count'];
		$this->setProperty('exp', $old + $cnt);
		return true;
	}

	public function AddLose($cnt)
	{
		$this->getProfile();
		$cnt = (int)max(0,$cnt);
		$old = (int)$this->data['lose_count'];
		$this->setProperty('exp', $old + $cnt);
		return true;
	}

    public function create($user_id, $social) {
        
            $rpc = RPC::_getInstance();
            $first_name = mysql_escape_string($rpc->get('first_name', ''));
            $last_name = mysql_escape_string($rpc->get('last_name', ''));
            $sex = (int) ($rpc->get('sex', 0));
            $photo = mysql_escape_string($rpc->get('photo', ''));

            $query = 'INSERT INTO `user_social` SET
                `user_id`=' . $user_id . ',`user_type`=' . $social . '';

            _database::query($query);
            $inserted_id = mysql_insert_id();
    

            $query = 'INSERT INTO `user` SET
                `user_id`=' . $inserted_id . ',
                `first_name`=\'' . $first_name . '\',
                `last_name`=\'' . $last_name . '\',
				`createDate`='.time().',
                `sex`='.$sex.',
				`money` ='.conf::$default_money.',
				`exp` ='.conf::$default_exp.',
                `photo`=\''.$photo.'\'';
            $r = _database::query($query);
            
            if($r && $inserted_id)
            {
                $this->profileLoaded = true;
                $this->data = array(
					'user_id' => $inserted_id,
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'sex'=>$sex,
                    'photo'=>$photo,
					'createDate'=> time(),
					'lastLogin' => time(),
					'money' => conf::$default_money,
					'exp' => conf::$default_exp,
					'current_game' => 0,
					'won_count' => 0,
					'lose_count' => 0
                );
                return $inserted_id;
            }
            return false;
       
    }

}